<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Common\Model\LiveFriendCommentModel;

/**
 * Description of WemeController
 *
 * @author
 */
class FriendCommentController extends AdminbaseController {

    function _initialize() {

        parent::_initialize();
        $this->_mod = D('LiveFriendComment');
    }

    function index() {
        $map = $this->_search();
        $mod = $this->_mod;
        if (!empty($mod)) {
            $result = $this->_list($mod, $map);
//        return    print_r($result);
            $this->assign('page', $result['page']);
            $list = $result['list'];
            $this->assign('list', $list);
            $this->display();
        }
    }

    public function edit() {
        $map['pid'] = $this->_request('pid');
        $data['subject'] = $this->_request('subject');
        $res = $this->_mod->where($map)->save($data);
        $res ? $this->ajaxReturn(array("status" => 1, "info" => "编辑成功")) : $this->ajaxReturn(array("status" => 0, "info" => "编辑失败！"));
    }

    public function delete() {
        $map['pid'] = $this->_request('pid');
        $tid=$this->_mod->where($map)->getField('tid');
        $map['tid']=$tid;
        D('LiveFriendCircle')->where($map)->setDec('replies');
        $res = $this->_mod->where($map)->delete();
        $res ? $this->ajaxReturn(array("status" => 1, "info" => "删除成功")) : $this->ajaxReturn(array("status" => 0, "info" => "删除失败！"));
    }

    protected function _list($model, $map = array(), $sort_by = '', $order_by = '', $field_list = '*', $pagesize = 20) {
        //排序
        $mod_pk = "pid";
        if ($this->_request("sort", 'trim')) {
            $sort = $this->_request("sort", 'trim');
        } else if (!empty($sort_by)) {
            $sort = $sort_by;
        } else if ($this->sort) {
            $sort = $this->sort;
        } else {
            $sort = $mod_pk;
        }
        if ($this->_request("order", 'trim')) {
            $order = $this->_request("order", 'trim');
        } else if (!empty($order_by)) {
            $order = $order_by;
        } else if ($this->order) {
            $order = $this->order;
        } else {
            $order = 'DESC';
        }
        $result = array();
        //如果需要分页
        if ($pagesize) {
            $count = $model->where($map)->count($mod_pk);
            $pager = $this->page($count, $pagesize);
        }
        $select = $model->field($field_list)->where($map)->order($sort . ' ' . $order);
        $this->list_relation && $select->relation(true);
        if ($pagesize) {
            $select->limit($pager->firstRow . ',' . $pager->listRows);
            $page = $pager->show('Admin');
            $result['page'] = $page;
            $this->assign("page", $page);
        }
        $list = $select->select();
        $result['list'] = $list;
        $this->assign('list_table', true);
        return $result;
    }

    protected function _search() {
        $map = array();
        ($tid = $this->_request('tid', 'trim')) && $map['tid'] = $tid;
        ($author = $this->_request('author', 'trim')) && $map['author'] = array('like', "%$author%");
        ($time_start = $this->_request('stime', 'trim')) && $map['dateline'] = array(array('egt', strtotime($time_start)), array('lt', strtotime($this->_request('etime', 'trim'))));
        $this->assign('search', array(
            'author' => $this->_request('author'),
            'stime' => $this->_request('stime'),
            'etime' => $this->_request('etime'),
            'tid' => $this->_request('tid'),
        ));
        return $map;
    }

    protected function _request($parama) {

        return I($parama);
    }

}
